﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.TakerMangement
{
    public class StatisticalReportSendController : Controller
    {
        //
        // GET: /StatisticalReportSend/
        ServiceReference1.Service1Client DCXT = new ServiceReference1.Service1Client();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取当前操作员拥有商户的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getMer()
        {
            try
            {
                Page<Model.Order_Mer_T> page = new Page<Model.Order_Mer_T>();
                List<Model.Order_Mer_T> list = new List<Model.Order_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = DCXT.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    list = ParseClass<Model.Order_Mer_T>.JsonList(jieguo);

                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list//返回当前页的数据
                    };
                    return Json(list, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取订单状态信息
        /// </summary>
        /// <returns></returns>
        public ActionResult getOrderFormType()
        {
            try
            {
                List<Model.Order_FormControl_T> list_Order_FormType = new List<Model.Order_FormControl_T>();//定义商品分类设置
                string result = DCXT.GetOrder_FormControl(GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    list_Order_FormType = ParseClass<Model.Order_FormControl_T>.JsonList(jieguo);//解析返回的结果\
                    return Json(list_Order_FormType, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json("加载异常！");
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        // 统计列表
        public ActionResult StatisticalReport(string MerID, string ConID, string TimeS, string TimeE)
        { 
            try
            {
                TimeS = TimeS.Replace("-", "");
                TimeE = TimeE.Replace("-", "");
                List<Model.PsBB> list = new List<Model.PsBB>();
                string result = DCXT.CountPs(MerID, ConID, TimeS, TimeE,  GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        list = ParseClass<Model.PsBB>.JsonList(jieguo);//解析返回的结果\
                        //foreach (Model.View_Order_Form item in list)
                        //{
                        //    item.FormTime = DateTimeHelper.ToDate2(item.FormTime);
                        //    if (item.SenTime.Length == 14)
                        //    {
                        //        item.SenTime = DateTimeHelper.ToDate2(item.SenTime);
                        //    }
                        //}
                    }
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = "0",
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }
    }
}
